#ifndef MESSAGEBOX_H
#define MESSAGEBOX_H

#include <QMessageBox>
#include <QInputDialog>
#include <QIcon>

#define SystemIcon QIcon(":/icon/hwsys.png")
class Message
{
public:
    static QMessageBox::StandardButton showInformation(QWidget *parent, const QString &title, const QString &text)
	{
        QMessageBox messageBox(QMessageBox::Information, title, text, QMessageBox::NoButton, parent);
        messageBox.setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
        messageBox.setWindowIcon(SystemIcon);
		messageBox.setButtonText(QMessageBox::Ok, "确认");

		if (messageBox.exec() == -1)
			return QMessageBox::Cancel;
		return messageBox.standardButton(messageBox.clickedButton());
	}

    static QMessageBox::StandardButton showWarning(QWidget *parent, const QString &title, const QString &text)
	{
        QMessageBox messageBox(QMessageBox::Warning, title, text, QMessageBox::NoButton, parent);
        messageBox.setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
        messageBox.setWindowIcon(SystemIcon);
		messageBox.setButtonText(QMessageBox::Ok, "确认");

		if (messageBox.exec() == -1)
			return QMessageBox::Cancel;
		return messageBox.standardButton(messageBox.clickedButton());
	}

    static QMessageBox::StandardButton showError(QWidget *parent, const QString &title, const QString &text)
	{
        QMessageBox messageBox(QMessageBox::Critical, title, text, QMessageBox::NoButton, parent);
        messageBox.setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
        messageBox.setWindowIcon(SystemIcon);
		messageBox.setButtonText(QMessageBox::Ok, "确认");

		if (messageBox.exec() == -1)
			return QMessageBox::Cancel;
		return messageBox.standardButton(messageBox.clickedButton());
	}

    static QMessageBox::StandardButton showQuestion(QWidget *parent, const QString &title, const QString &text)
	{
		QMessageBox messageBox(parent);
        messageBox.setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
        messageBox.setWindowIcon(SystemIcon);
		messageBox.setWindowTitle(title);
		messageBox.setText(text);
		messageBox.setIcon(QMessageBox::Question);

		messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
		messageBox.setDefaultButton(QMessageBox::No);
		messageBox.setButtonText(QMessageBox::Yes, "Yes");
		messageBox.setButtonText(QMessageBox::No, "No");

		if (messageBox.exec() == -1)
			return QMessageBox::Cancel;
		return messageBox.standardButton(messageBox.clickedButton());
	}

    static int showSelect(QWidget *parent, const QString &title, const QString &text, const QStringList &select)
	{
		QMessageBox messageBox(parent);
        messageBox.setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint | Qt::MSWindowsFixedSizeDialogHint);
        messageBox.setWindowIcon(SystemIcon);
		messageBox.setWindowTitle(title);
		messageBox.setText(text);
		messageBox.setIcon(QMessageBox::Question);

		if (select.length() == 0)
		{
			return -1;
		}
		else if (select.length() == 1)
		{
			messageBox.setStandardButtons(QMessageBox::Yes);
			messageBox.setButtonText(QMessageBox::Yes, select.at(0));
		}
		else if (select.length() == 2)
		{
			messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
			messageBox.setButtonText(QMessageBox::Yes, select.at(0));
			messageBox.setButtonText(QMessageBox::No, select.at(1));
		}
		else if (select.length() >= 3)
		{
            messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Abort);
			messageBox.setButtonText(QMessageBox::Yes, select.at(0));
			messageBox.setButtonText(QMessageBox::No, select.at(1));
            messageBox.setButtonText(QMessageBox::Abort, select.at(2));
		}
		messageBox.setDefaultButton(QMessageBox::Yes);

		if (messageBox.exec() == -1)
            return -1;
		else
		{
            QMessageBox::StandardButton retCode = messageBox.standardButton(messageBox.clickedButton());
            if (QMessageBox::Yes == retCode)        return 0;
            else if (QMessageBox::No == retCode)    return 1;
            else if (QMessageBox::Abort == retCode) return 2;
		}
		return -1;
	}

    static QString getInput(QWidget *parent, const QString &title, const QString &label, const QString &text = QString(), Qt::InputMethodHints inputMethodHints = Qt::ImhNone)
	{
		QInputDialog dialog(parent);
        dialog.setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
        dialog.setWindowIcon(SystemIcon);
		dialog.setWindowModality(Qt::WindowModal);
		dialog.setWindowTitle(title);
		dialog.setLabelText(label);
		dialog.setTextValue(text);
		dialog.setTextEchoMode(QLineEdit::Password);
		dialog.setInputMethodHints(inputMethodHints);

		dialog.setStyleSheet("font-size: 10pt;");
		dialog.setOkButtonText("确定");
		dialog.setCancelButtonText("返回");

		int ret = dialog.exec();
        if (ret)    return dialog.textValue();
        else        return QString();
	}

    static QString getInputInt(QWidget *parent, const QString &title, const QString &label, const QString &text = QString(), Qt::InputMethodHints inputMethodHints = Qt::ImhNone)
	{
		QInputDialog dialog(parent);
        dialog.setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
        dialog.setWindowIcon(SystemIcon);
		dialog.setWindowModality(Qt::WindowModal);
		dialog.setWindowTitle(title);
		dialog.setLabelText(label);
		dialog.setTextValue(text);
		dialog.setInputMethodHints(inputMethodHints);

		dialog.setStyleSheet("font-size: 10pt;");
		dialog.setOkButtonText("确定");
		dialog.setCancelButtonText("返回");

		int ret = dialog.exec();
        if (ret)    return dialog.textValue();
        else        return QString();
	}
};

#endif // MESSAGEBOX_H
